package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper dtsAdminMapper;
    @Override
    public DtsAdmin findByUsername(String username) {
        //根据用户名查询用户对象
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(DtsAdmin::getUsername,username);
        DtsAdmin admin = dtsAdminMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public IPage<DtsAdmin> queryPage(Integer page, Integer limit, String sort, String order, String username) {
        //创建分页条件参数
        IPage<DtsAdmin> ipage = new Page(page,limit);
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        //创建查询条件
        //如果username不为null 或者"" 则添加条件
        if (!StringUtils.isEmpty(username)){
            qw.like("username",username);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else{
                qw.orderByAsc(sort);
            }
        }
        dtsAdminMapper.selectPage(ipage,qw);
        return ipage;
    }

    @Override
    public DtsAdmin add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);//没有被逻辑删除


        //对密码进行加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());

        admin.setPassword(encode);
        dtsAdminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        //从前端获取的面
        String password = admin.getPassword();
        //冲数据库中获取的密码
        DtsAdmin admin1 = dtsAdminMapper.selectById(admin.getId());
        String password1 = admin1.getPassword();
        //如果相同  则说明前端没有修改密码  不需要更新
        if (!password.equalsIgnoreCase(password1)){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(encode);
        }
        admin.setUpdateTime(new Date());
        dtsAdminMapper.updateById(admin);
        return admin;
    }

    @Override
    public void deleteById(DtsAdmin admin) {
        dtsAdminMapper.deleteById(admin.getId());
    }
}
